﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using demo.Common;
using demo.Entity;
using demo.IService;
using demo.Service;
using iAgileFramework.DataTransCarrier;

namespace demo.BLL
{
    public class UserBLL:BLLBase
    {
        private const string LoginValCode = "LoginValCode";
        public UserService service = new UserService();

        #region 登录
        /// <summary>
        /// 获得登录验证码
        /// </summary>
        /// <returns></returns>
        public byte[] GetLoginValCode()
        {
            var checkCode = new ValidateCode();
            var code = checkCode.CreateValidateCode(5);
            HttpContext.Current.Session[LoginValCode] = code;
            byte[] bytes = checkCode.CreateValidateGraphic(code);
            return bytes;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="account">账号</param>
        /// <param name="password">密码</param>
        /// <param name="checkCode">验证码</param>
        /// <param name="isLogin">是否登录</param>
        /// <returns></returns>
        public ReturnValue DoLogin(string account, string password, string checkCode, bool isLogin = true)
        {
            var rv = new ReturnValue();
            if (string.IsNullOrEmpty(account))
            {
                return rv.Fail("账号不能为空！");
            }
            if (string.IsNullOrEmpty(password))
            {
                return rv.Fail("密码不能为空！");
            }
            if (isLogin)
            {
                if (string.IsNullOrWhiteSpace(checkCode))
                {
                    return rv.Fail("验证码不能为空！");
                }
                if (HttpContext.Current.Session[LoginValCode] == null)
                {
                    return rv.Fail("验证码失效，请重新获取！");
                }
                if (HttpContext.Current.Session[LoginValCode].ToString() != checkCode)
                {
                    return rv.Fail("验证码不一致，请重新输入！");
                }
            }

            Users model = service.GetByAccAndPwd(account, DESEncrypt.Encrypt(password));
            if (model == null)
            {
                return rv.Fail("账号或密码错误！");
            }
            if (model.Status == Status.Disabled)
            {
                return rv.Fail("账号已被禁用！");
            }

            Context.user = model;
            string cookie = DESEncrypt.Encrypt(account + "|" + Utils.IPAddress);
            //保存cookie
            Cookie.setCookie(Context.CookieName, HttpUtility.UrlEncode(cookie), 30);
            return rv.Success(cookie);
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="account">账号</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public ReturnValue DoLogin(string account, string password)
        {
            var rv = new ReturnValue();
            if (string.IsNullOrEmpty(account))
            {
                return rv.Fail("账号不能为空！");
            }
            if (string.IsNullOrEmpty(password))
            {
                return rv.Fail("密码不能为空！");
            }            

            Users model = service.GetByAccAndPwd(account, DESEncrypt.Encrypt(password));
            if (model == null)
            {
                return rv.Fail("账号或密码错误！");
            }
            if (model.Status == Status.Disabled)
            {
                return rv.Fail("账号已被禁用！");
            }

            Context.user = model;
            string cookie = DESEncrypt.Encrypt(account + "|" + Utils.IPAddress);
            //保存cookie
            Cookie.setCookie(Context.CookieName, HttpUtility.UrlEncode(cookie), 30);
            return rv.Success(cookie);
        }

        #endregion

        #region 注册
        /// <summary>
        /// 是否已存在账号
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public bool IsExists(string account)
        {
            bool i = service.IsExists(account);
            return i;
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public ReturnValue DoRegister(Users model)
        {
            var rv = new ReturnValue();
            model.Password = DESEncrypt.Encrypt(model.Password);
            int i = service.Add(model);
            return i > 0 ? rv.Success("注册成功！") : rv.Fail("注册失败！");
        } 
        #endregion

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="mpass">原密码</param>
        /// <param name="newpass">新密码</param>
        /// <returns></returns>
        public object ChangePwd(string mpass, string newpass)
        {
            var rv =new ReturnValue();
            Users model = Context.user;
            //if (DESEncrypt.Encrypt(mpass) != model.Password)
            //{
            //    return rv.Fail("原密码错误！");
            //}
            if (DESEncrypt.Decrypt(model.Password) != mpass)
            {
                return rv.Fail("原密码错误！");
            }
            if (DESEncrypt.Encrypt(newpass) == model.Password)
            {
                return rv.Fail("新密码不能与原密码一样！");
            }            
            if (service.UpdateModel(model))
            {
                model.Password = DESEncrypt.Encrypt(newpass);
                Context.user = model;                
                return rv.Success("操作成功！");
            }
            else
            {
                return rv.Fail("操作失败！");
            }
        }
    }
}
